package com.bigchui.DesignPattern.Singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 注册式容器单例
 * Created by wangzhen on 2019/3/10.
 */
public class ContinerSingleton {
    private ContinerSingleton(){

    }
    private static Map<String,Object> ioc=new ConcurrentHashMap<>();
    public static Object getInstance(String className){
        if(!ioc.containsKey(className)){
            Object obj=null;
            try {
                obj=Class.forName(className).newInstance();
                ioc.put(className,obj);
            }catch (Exception e){
                e.printStackTrace();
            }
            return obj;
        }
        return ioc.get(className);
    }
}
